/**
 * Software License, Version 1.0
 * 
 * Copyright 2003 The Trustees of Indiana University.  All rights reserved.
 * 
 *
 *Redistribution and use in source and binary forms, with or without 
 *modification, are permitted provided that the following conditions are met:
 *
 *1) All redistributions of source code must retain the above copyright notice,
 * the list of authors in the original source code, this list of conditions and
 * the disclaimer listed in this license;
 *2) All redistributions in binary form must reproduce the above copyright 
 * notice, this list of conditions and the disclaimer listed in this license in
 * the documentation and/or other materials provided with the distribution;
 *3) Any documentation included with all redistributions must include the 
 * following acknowledgement:
 *
 *"This product includes software developed by the Community Grids Lab. For 
 * further information contact the Community Grids Lab at 
 * http://communitygrids.iu.edu/."
 *
 * Alternatively, this acknowledgement may appear in the software itself, and 
 * wherever such third-party acknowledgments normally appear.
 * 
 *4) The name Indiana University or Community Grids Lab or NaradaBrokering, 
 * shall not be used to endorse or promote products derived from this software 
 * without prior written permission from Indiana University.  For written 
 * permission, please contact the Advanced Research and Technology Institute 
 * ("ARTI") at 351 West 10th Street, Indianapolis, Indiana 46202.
 *5) Products derived from this software may not be called NaradaBrokering, 
 * nor may Indiana University or Community Grids Lab or NaradaBrokering appear
 * in their name, without prior written permission of ARTI.
 * 
 *
 * Indiana University provides no reassurances that the source code provided 
 * does not infringe the patent or any other intellectual property rights of 
 * any other entity.  Indiana University disclaims any liability to any 
 * recipient for claims brought by any other entity based on infringement of 
 * intellectual property rights or otherwise.  
 *
 *LICENSEE UNDERSTANDS THAT SOFTWARE IS PROVIDED "AS IS" FOR WHICH NO 
 *WARRANTIES AS TO CAPABILITIES OR ACCURACY ARE MADE. INDIANA UNIVERSITY GIVES
 *NO WARRANTIES AND MAKES NO REPRESENTATION THAT SOFTWARE IS FREE OF 
 *INFRINGEMENT OF THIRD PARTY PATENT, COPYRIGHT, OR OTHER PROPRIETARY RIGHTS. 
 *INDIANA UNIVERSITY MAKES NO WARRANTIES THAT SOFTWARE IS FREE FROM "BUGS", 
 *"VIRUSES", "TROJAN HORSES", "TRAP DOORS", "WORMS", OR OTHER HARMFUL CODE.  
 *LICENSEE ASSUMES THE ENTIRE RISK AS TO THE PERFORMANCE OF SOFTWARE AND/OR 
 *ASSOCIATED MATERIALS, AND TO THE PERFORMANCE AND VALIDITY OF INFORMATION 
 *GENERATED USING SOFTWARE.
 */
package cgl.narada.wsinfra.benchmark;

import java.util.Calendar;

import javax.xml.namespace.QName;
import javax.xml.soap.MessageFactory;
import javax.xml.soap.SOAPMessage;

import org.apache.xmlbeans.XmlCursor;

import cgl.narada.wsinfra.exception.WsFaultException;
import cgl.narada.wsinfra.schemas.addressing.ActionDocument;
import cgl.narada.wsinfra.schemas.addressing.EndpointReferenceType;
import cgl.narada.wsinfra.schemas.addressing.FromDocument;
import cgl.narada.wsinfra.schemas.addressing.RelatesToDocument;
import cgl.narada.wsinfra.schemas.addressing.ToDocument;
import cgl.narada.wsinfra.schemas.eventing.FilterType;
import cgl.narada.wsinfra.schemas.eventing.GetStatusDocument;
import cgl.narada.wsinfra.schemas.eventing.GetStatusResponseDocument;
import cgl.narada.wsinfra.schemas.eventing.RenewDocument;
import cgl.narada.wsinfra.schemas.eventing.RenewResponseDocument;
import cgl.narada.wsinfra.schemas.eventing.SubscribeResponseDocument;
import cgl.narada.wsinfra.schemas.eventing.SubscriptionEndDocument;
import cgl.narada.wsinfra.schemas.soap.EnvelopeDocument;
import cgl.narada.wsinfra.schemas.soap.EnvelopeType;
import cgl.narada.wsinfra.schemas.wsrm.SequenceAcknowledgementDocument;
import cgl.narada.wsinfra.schemas.wsrm.SequenceDocument;
import cgl.narada.wsinfra.schemas.wsrm.SequenceFaultDocument;
import cgl.narada.wsinfra.schemas.wsrm.TerminateSequenceDocument;
import cgl.narada.wsinfra.util.ExceptionToFaultConversion;
import cgl.narada.wsinfra.util.SoapEnvelopeConversion;
import cgl.narada.wsinfra.util.SoapMessageAlteration;
import cgl.narada.wsinfra.wsa.AddressingHeaders;
import cgl.narada.wsinfra.wsa.ParseWsaHeaders;
import cgl.narada.wsinfra.wsa.WsaEnvelopeCreator;
import cgl.narada.wsinfra.wsa.WsaEprCreator;
import cgl.narada.wsinfra.wsa.WsaProcessingFactory;
import cgl.narada.wsinfra.wse.WseActions;
import cgl.narada.wsinfra.wse.WseElementCreation;
import cgl.narada.wsinfra.wse.WseProcessingFactory;
import cgl.narada.wsinfra.wse.WseQNames;
import cgl.narada.wsinfra.wse.WseRequestCreator;
import cgl.narada.wsinfra.wse.WseResponseCreator;
import cgl.narada.wsinfra.wse.impl.WseSinkProcessor;
import cgl.narada.wsinfra.wse.matching.MatchingCapability;
import cgl.narada.wsinfra.wse.matching.MatchingCapabilityFactory;
import cgl.narada.wsinfra.wse.matching.SubscriptionEntry;
import cgl.narada.wsinfra.wse.matching.SubscriptionEntryFactory;
import cgl.narada.wsinfra.wse.matching.capability.CapabilityRegistration;
import cgl.narada.wsinfra.wsrm.WsrmElementAddition;
import cgl.narada.wsinfra.wsrm.WsrmElementCreation;
import cgl.narada.wsinfra.wsrm.WsrmFaults;
import cgl.narada.wsinfra.wsrm.WsrmProcessingFactory;
import cgl.narada.wsinfra.wsrm.WsrmRequestCreator;
import cgl.narada.wsinfra.wsrm.WsrmResponseCreator;
import cgl.narada.wsinfra.wsrm.impl.WsrmSourceNode;

/**This is a class which performs some of the operations that we will be
   benchmarking. 

   @author Shrideep Pallickara
   $Date: 2005/07/29 22:41:29 $
   $Revision: 1.3 $
*/


public class BenchmarkOperations {
  
  private WsaEprCreator wsaEprCreator;
  private WsaEnvelopeCreator wsaEnvelopeCreator;
  private ParseWsaHeaders parseWsaHeaders;

  private SoapEnvelopeConversion soapEnvelopeConversion;
  private SoapMessageAlteration soapMessageAlteration;
  private ExceptionToFaultConversion faultConverter;

  private WseActions wseActions;

  private WsrmFaults wsrmFaults;
  private WsrmSourceNode wsrmSourceNode;
  private WsrmRequestCreator wsrmRequestCreator;
  private WsrmResponseCreator wsrmResponseCreator;
  private WsrmElementCreation wsrmElementCreation;
  private WsrmElementAddition wsrmElementAddition;
  
  private WseRequestCreator wseRequestCreator;
  private WseResponseCreator wseResponseCreator;
  private WseElementCreation wseElementCreation;
  private EndpointReferenceType wseSinkEpr, wseSourceEpr, wseSubManagerEpr, wseEndToEpr;
  private SubscriptionEntry wseSubscriptionEntry;
  private EnvelopeDocument wseNotificationEnvelope;
  private FilterType xpathFilter, topicFilter, regExFilter, xqueryFilter = null;
  private int tracker = 0;
  
  
  private MessageFactory messageFactory;


  /** Benchmarking variables. These are entities that will be used by various
      benchmarking operations. If for example we are trying to measure the 
      amount of time it takes to convert a SOAPMessage into an 
      EnvelopeDocument we should not be including the time it takes to create 
      the SOAPMessage. */
  private EnvelopeDocument envelopeDocumentForConversion;
  private SOAPMessage soapMessageForConversion;
  private EndpointReferenceType eprForWsaEnvelope;
  private EndpointReferenceType eprWithRefProps;
  private EnvelopeDocument envelopeDocumentForParsingWsaHeaders;
  
  private ToDocument wsrmToDocument;
  private AddressingHeaders addressingHeaders;
  private SequenceDocument wsrmSequenceDocument;
  private long[] wsrmMessageNumbersToAck= {1,2,3,4,5,7,8,9, 10, 13,14,16,18};
  private String wsrmSequenceIdentifier;

  private String moduleName = "BenchmarkOperations: ";
  private static final BenchmarkOperations instance =new BenchmarkOperations();

  
  private BenchmarkOperations() {
    wsaEprCreator = WsaProcessingFactory.getWsaEprCreator();
    wsaEnvelopeCreator = WsaProcessingFactory.getWsaEnvelopeCreator();
    parseWsaHeaders = WsaProcessingFactory.getParseWsaHeaders();

    soapEnvelopeConversion = SoapEnvelopeConversion.getInstance();
    soapMessageAlteration = SoapMessageAlteration.getInstance();
    faultConverter = ExceptionToFaultConversion.getInstance();

    wseActions = WseActions.getInstance();
    wsrmFaults = WsrmProcessingFactory.getWsrmFaults();
    wsrmRequestCreator =  WsrmProcessingFactory.getWsrmRequestCreator();
    wsrmResponseCreator = WsrmProcessingFactory.getWsrmResponseCreator();
    wsrmElementCreation = WsrmProcessingFactory.getWsrmElementCreation();
    wsrmElementAddition =  WsrmProcessingFactory.getWsrmElementAddition();

    wseRequestCreator = WseProcessingFactory.getWseRequestCreator();
    wseResponseCreator = WseProcessingFactory.getWseResponseCreator();
    wseElementCreation = WseProcessingFactory.getWseElementCreation();
    
    try {
      messageFactory = MessageFactory.newInstance();
      initializeBenchmarkVariables();
    } catch (Exception e) {
      String errorReport = moduleName + "Unable to INITIALIZE either the " + 
	"SoapMessageFactory AND/OR the benchmarking variables\n" +
	e.toString();
      System.out.println(errorReport);
    }
    
  }
  
  /** This method initializes various variables and types that are utilized
      by the benchmark oeprations. */
  private void initializeBenchmarkVariables() throws Exception {
    envelopeDocumentForConversion = createSubscribeRequest();
    soapMessageForConversion = convertEnvelopeDocument();
    eprForWsaEnvelope = createEpr();
    eprWithRefProps = createEprWithReferenceProperties();
    envelopeDocumentForParsingWsaHeaders = createEprEnvelopeWithMostWSAFields();

    wsrmSequenceIdentifier = "3afca123-6aae-43ba-bef4-25bcc3a995ce";
    wsrmToDocument = ToDocument.Factory.newInstance();
    wsrmToDocument.addNewTo().setStringValue("http://www.wsrm.sink.traffic");
    addressingHeaders = parseWsaHeaders();
    wsrmSequenceDocument = createSequenceDocument();
        
    String sourceAddress = "tcp://deccan.ucs.indiana.edu:19000";
	 wseSourceEpr = wsaEprCreator.createEpr(sourceAddress);
	 String sinkAddress = "tcp://deccan.ucs.indiana.edu:21000";
	 wseSinkEpr = wsaEprCreator.createEpr(sinkAddress);
	 String subManagerAddress = "tcp://deccan.ucs.indiana.edu:19000";
	 wseSubManagerEpr = wsaEprCreator.createEpr(subManagerAddress);
	 wseEndToEpr = wsaEprCreator.createEpr(sourceAddress);
	 
	 SubscriptionEntryFactory subscriptionEntryFactory =
		  SubscriptionEntryFactory.getInstance();
	  
	 String subscriptionIdentifier = "uuid:e1886c5c-5e86-48d1-8c77-fc1c28d47180";
	 FilterType filterType = FilterType.Factory.newInstance();
	 String topicFilterDialect = "http://www.naradabrokering.org/TopicMatching";
	 filterType.setDialect(topicFilterDialect);
	 
	 Calendar expiresAt = Calendar.getInstance();
	 expiresAt.add(Calendar.MONTH, 1);
	 
	 wseSubscriptionEntry =
		 subscriptionEntryFactory.createEntry(subscriptionIdentifier,filterType,expiresAt,
				 wseSourceEpr, wseSinkEpr, wseEndToEpr, wseSubManagerEpr);
	 
	 wseNotificationEnvelope = createWseNotificationEnvelope();
	 initializeFilterTypes();
  }

  public static BenchmarkOperations getInstance() {
    return instance;
  }

  /** Creates a basic envelope document. */ 
  public EnvelopeDocument createEnvelopeDocument() {
    EnvelopeDocument envelopeDocument = EnvelopeDocument.Factory.newInstance();
    EnvelopeType envelopeType = envelopeDocument.addNewEnvelope();
    envelopeType.addNewHeader();
    envelopeType.addNewBody();
    
    return envelopeDocument;
  }
  

  /** Creates a basic SOAP message. */ 
  public SOAPMessage createSOAPMessage() throws Exception {
    SOAPMessage soapMessage = messageFactory.createMessage();
    return soapMessage;
  }

  
  /** Converts an EnvelopeDocument into a SOAPMessage. */
  public SOAPMessage convertEnvelopeDocument() throws Exception {    
    SOAPMessage soapMessage = 
      soapEnvelopeConversion.getSOAPMessage(envelopeDocumentForConversion);
    return soapMessage;
  }


  /** Converts a SOAPMessage into an EnvelopeDocument. */
  public EnvelopeDocument convertSOAPMessage() 
    throws Exception {
    EnvelopeDocument envelopeDocument = 
      soapEnvelopeConversion.getEnvelopeDocument(soapMessageForConversion);
    return envelopeDocument;
  }


  public EndpointReferenceType createEpr() {
    String address = "http://communications.ucs.indiana.edu:19000";
    EndpointReferenceType eprType = wsaEprCreator.createEpr(address);
    return eprType;
  }


  public EndpointReferenceType createEprWithReferenceProperties() {
    String address = "http://www.other.example.com/OnStormWarning";
    EndpointReferenceType eprType = wsaEprCreator.createEpr(address);
    QName qName = new QName("http://www.example.com/warnings",
			    "MySubscription");
    wsaEprCreator.addElementToReferenceProperties(eprType, qName, "25");
    return eprType;
  }



  public EnvelopeDocument createEprEnvelope() {
    FromDocument from = null;
    ActionDocument action = null;
    RelatesToDocument relatesTo = null;
    
    EnvelopeDocument envDoc = 
      wsaEnvelopeCreator.createSoapEnvelope(eprWithRefProps, from, 
					    action, relatesTo);

    return envDoc;
  }



  public EnvelopeDocument createEprEnvelopeWithMostWSAFields() {
    EndpointReferenceType fromEpr = createEpr();
    FromDocument from = FromDocument.Factory.newInstance();
    from.setFrom(fromEpr);
    
    ActionDocument action = wseActions.getRenewResponseAction();
    RelatesToDocument relatesTo = RelatesToDocument.Factory.newInstance();
    relatesTo.addNewRelatesTo().setStringValue("uuid:22e8a584-0d18-4228-b2a8-3716fa2097fa");
    
    EnvelopeDocument envDoc = 
      wsaEnvelopeCreator.createSoapEnvelope(eprForWsaEnvelope, from, 
					    action, relatesTo);

    return envDoc;
  }


  public EnvelopeDocument createSubscribeRequest() throws Exception {
    String sinkAddress = "tcp://deccan.ucs.indiana.edu:21000";
    String sourceAddress = "tcp://deccan.ucs.indiana.edu:19000";
    EndpointReferenceType sourceEpr = wsaEprCreator.createEpr(sourceAddress);
    String deliveryMode = "http://schemas.xmlsoap.org/ws/2004/08/eventing/DeliveryModes/Push";
    String topicFilterDialect = "http://www.naradabrokering.org/TopicMatching";
    String topicFilterConstraint = "/Literature/Shakespere";
    String filterConstraint = topicFilterConstraint;
    String filterDialect = topicFilterDialect;

    

    EndpointReferenceType sinkEpr = wsaEprCreator.createEpr(sinkAddress);
    EndpointReferenceType endTo = wsaEprCreator.createEpr(sinkAddress);
    Calendar expiresAt = Calendar.getInstance();
    expiresAt.add(Calendar.MONTH, 1);


    WseSinkProcessor wseSinkProcessor = WseSinkProcessor.getInstance();
    wseSinkProcessor.setSinkEPR(sinkEpr);
    
    EnvelopeDocument envelopeDocument = 
      wseSinkProcessor.createSubscribeRequest(sourceEpr, deliveryMode, endTo,
					      filterDialect, filterConstraint,
					      expiresAt);

    return envelopeDocument;
  }


  public EnvelopeDocument createSubscribeResponse() throws Exception {
	  RelatesToDocument relatesTo = RelatesToDocument.Factory.newInstance();
	  String messageId = "uuid:e1886c5c-5e86-48d1-8c77-fc1c28d47180";
	  relatesTo.addNewRelatesTo().setStringValue(messageId);
	  
	  SubscribeResponseDocument subResponseDocument = 
		  createSubscribeResponseDocument();
	  wseResponseCreator.createSubscribeResponse(wseSinkEpr, wseSourceEpr, relatesTo,
			  subResponseDocument);
	  return null;
  }
  
  
  public SubscribeResponseDocument createSubscribeResponseDocument() throws Exception {	  
	  SubscribeResponseDocument subscribeResponseDocument = 
		  wseElementCreation.newSubscribeResponseDocument(wseSubscriptionEntry);

	  return subscribeResponseDocument;
  }
  
  
  public EnvelopeDocument createRenewRequest() throws Exception {
	  String sinkAddress = "tcp://deccan.ucs.indiana.edu:21000";
	  String subManagerAddress = "tcp://deccan.ucs.indiana.edu:19000";
	  EndpointReferenceType subManagerEpr = wsaEprCreator.createEpr(subManagerAddress);
	  EndpointReferenceType sinkEpr = wsaEprCreator.createEpr(sinkAddress);
	  
	  RenewDocument renewDocument = createRenewDocument();
	  
	  EnvelopeDocument envelopeDocument =
		  wseRequestCreator.createRenew(subManagerEpr, sinkEpr,renewDocument );
	  return envelopeDocument;
  }

  
  public RenewDocument createRenewDocument() throws Exception {
	  Calendar expiresAt = Calendar.getInstance();
	  expiresAt.add(Calendar.MONTH, 3);	
	  
	  RenewDocument renewDocument = wseElementCreation.newRenewDocument(expiresAt);
	  return renewDocument;
  }
  
  
  public EnvelopeDocument createRenewResponse() throws Exception {
	  RelatesToDocument relatesTo = RelatesToDocument.Factory.newInstance();
	  String messageId = "uuid:e1886c5c-5e86-48d1-8c77-fc1c28d47180";
	  relatesTo.addNewRelatesTo().setStringValue(messageId);
	  
	  RenewResponseDocument renewResponseDocument =
		  createRenewResponseDocument();
	  
	  EnvelopeDocument envelopeDocument = 
		  wseResponseCreator.createRenewResponse(wseSinkEpr, wseSubManagerEpr,
				  relatesTo, renewResponseDocument);
	  return envelopeDocument;
  }

  public RenewResponseDocument createRenewResponseDocument() throws Exception {
	  RenewResponseDocument renewResponseDocument =
		  wseElementCreation.newRenewResponseDocument(wseSubscriptionEntry);
	  return renewResponseDocument;
  }
  
  
  public EnvelopeDocument createUnsubscribe() throws Exception {
	  EnvelopeDocument envelopeDocument =
		  wseRequestCreator.createUnsubscribe(wseSubManagerEpr, wseSinkEpr);
	  return envelopeDocument;
  }
  
  
  public EnvelopeDocument createUnsubscribeResponse() throws Exception {
	  RelatesToDocument relatesTo = RelatesToDocument.Factory.newInstance();
	  String messageId = "uuid:e1886c5c-5e86-48d1-8c77-fc1c28d47180";
	  relatesTo.addNewRelatesTo().setStringValue(messageId);
	  EnvelopeDocument envelopeDocument =
		  wseResponseCreator.createUnsubscribeResponse(wseSinkEpr, wseSubManagerEpr, 
				  relatesTo);
	  
	  return envelopeDocument;
  }
  
  
  public EnvelopeDocument createGetStatus() throws Exception {
	  EnvelopeDocument envelopeDocument =
		  wseRequestCreator.createGetStatus(wseSubManagerEpr, wseSinkEpr, 
				  createGetStatusDocument());
	  return envelopeDocument;
  }
  
  
  public GetStatusDocument createGetStatusDocument() throws Exception {
	  return wseElementCreation.newGetStatusDocument();
  }
  
  public EnvelopeDocument createGetStatusResponse() throws Exception {
	  RelatesToDocument relatesTo = RelatesToDocument.Factory.newInstance();
	  String messageId = "uuid:e1886c5c-5e86-48d1-8c77-fc1c28d47180";
	  relatesTo.addNewRelatesTo().setStringValue(messageId);
	  
	  GetStatusResponseDocument getStatusResponseDocument =
		  createGetStatusResponseDocument();
	  
	  EnvelopeDocument envelopeDocument = 
		  wseResponseCreator.createGetStatusResponse(wseSinkEpr, wseSubManagerEpr,
				  relatesTo, getStatusResponseDocument);
	  return envelopeDocument;
  }
  
  
  public GetStatusResponseDocument createGetStatusResponseDocument() throws Exception {
	  GetStatusResponseDocument getStatusResponseDocument =
		  wseElementCreation.newGetStatusResponseDocument(wseSubscriptionEntry);
	  return getStatusResponseDocument;
  }
  

  public EnvelopeDocument createSubscriptionEnd() throws Exception {
	  SubscriptionEndDocument subscriptionEndDocument =
		  createSubscriptionEndDocument();
	  EnvelopeDocument envelopeDocument = 
		  wseResponseCreator.createSubscriptionEnd(wseEndToEpr, wseSourceEpr,
				  subscriptionEndDocument);
	  return envelopeDocument;
  }
  
  
  public SubscriptionEndDocument createSubscriptionEndDocument() throws Exception {
	  String status = "Subscription has been terminated";
	  String reason ="The subscription has expired.";
	  
	  SubscriptionEndDocument subscriptionEndDocument =
		  wseElementCreation.newSubscriptionEndDocument(wseSubscriptionEntry,status, reason);
	  return subscriptionEndDocument;
  }
  
  
  public void performXPathMatching() throws Exception {
	  performMatching(xpathFilter);
  }
  
  public void performTopicMatching() throws Exception {
	  performMatching(topicFilter);	  
  }
  
  public void performRegExMatching() throws Exception {
	  performMatching(regExFilter);	  
  }
  
  public void performXQueryMatching() throws Exception {
	  performMatching(xqueryFilter);	  
  }
  
  
  
  private void performMatching(FilterType filter) throws Exception {
	  MatchingCapabilityFactory matchingCapabilityFactory =
		  MatchingCapabilityFactory.getInstance();
	  
	  MatchingCapability matchingCapability =
		  matchingCapabilityFactory.getCapability(filter.getDialect());
	  
	  matchingCapability.performMatching(wseNotificationEnvelope, filter);
  }
  
  
  private void initializeFilterTypes() {
	  String xpathDialect ="http://www.w3.org/TR/1999/REC-xpath-19991116";
	  String topicDialect =  "http://www.naradabrokering.org/TopicMatching";
	  String regExDialect = "http://www.naradabrokering.org/RegularExpressionMatching";
	  String xqueryDialect = "http://www.w3.org/TR/2004/WD-xquery-20041029";
	  	 
	  //String xpathFilterConstraint = "declare namespace s11='http://schemas.xmlsoap.org/soap/envelope/' c1='http://www.naradabrokering.org/catalog'" + "$this/s11:Envelope/s11:Body" + "/c1:catalog/c1:cd/c1:price";

	  String xpathFilterConstraint = "declare namespace c1='http://www.naradabrokering.org/catalog'" + "$this//c1:catalog/c1:cd/c1:price";
	  String topicFilterConstraint = "/Literature/Shakespere";
	  String regExFilterConstraint = "[Tt]he [Qq]uick [Bb]rown [Ff]ox [Jj]umps";
	  
	  String nsText = "declare namespace po='http://openuri.org/easypo'";
	  String pathText = "$this//po:purchase-order/po:line-item";
	  String xqueryFilterConstraint = nsText + pathText;
	  
	  xpathFilter = createFilter(xpathDialect,xpathFilterConstraint);
	  topicFilter = createFilter(topicDialect, topicFilterConstraint);
	  regExFilter = createFilter(regExDialect, regExFilterConstraint);
	  //xqueryFilter = createFilter(xqueryDialect, xqueryFilterConstraint);
	  
	  
	  CapabilityRegistration capabilityRegistration =
		  CapabilityRegistration.getInstance();
	  capabilityRegistration.registerCapabilities();
	  
  }
  
  
  
  private FilterType createFilter(String filterDialect, String filterConstraint) {
	  FilterType filter = FilterType.Factory.newInstance();
	  filter.setDialect(filterDialect);
	  
	  if (filterConstraint != null) {
		  XmlCursor filterCursor = filter.newCursor();
		  filterCursor.setTextValue(filterConstraint);
		  filterCursor.dispose();
	  }
	  return filter;
  }
  
  private EnvelopeDocument createWseNotificationEnvelope() {
	  String sourceAddress = "tcp://deccan.ucs.indiana.edu:19000";
	  EndpointReferenceType sourceEpr = wsaEprCreator.createEpr(sourceAddress);
	  FromDocument from = FromDocument.Factory.newInstance();
	  from.setFrom(sourceEpr);
	  EnvelopeDocument envelopeDocument = wsaEnvelopeCreator
																				.createSoapEnvelope(from);

	  QName qname = new QName("http://www.naradabrokering.org",
				"Application-Content");
	  XmlCursor bodyCursor = envelopeDocument.getEnvelope().getBody()
															.newCursor();
	  tracker++;
	  String value = "Tracker :" + tracker + "\n"
							+ "The quick brown fox jumps upon the lazy dog";
	  bodyCursor.toNextToken();

	  String catUri = "http://www.naradabrokering.org/catalog";
	  bodyCursor.insertElement("catalog", catUri);
	  bodyCursor.toPrevToken();
	  bodyCursor.insertElement("cd", catUri);
	  bodyCursor.toPrevToken();
	  bodyCursor.insertElementWithText("price", catUri, "11.90");

	  String xpathString = "<catalog> <cd> <title>Hide your heart</title> <artist>Bonnie Tyler</artist>  <country>UK</country>  <company>CBS Records</company> <price>11.90</price>  <year>1988</year>  </cd>  </catalog>";
		// bodyCursor.insertChars(xpathString);
	  bodyCursor.insertElementWithText(qname, value);
	  bodyCursor.dispose();

	  bodyCursor = envelopeDocument.getEnvelope().getBody().newCursor();
	  String poUri = "http://openuri.org/easypo";
	  bodyCursor.insertElement("purchase-order", poUri);
	  bodyCursor.toPrevToken();
	  bodyCursor.insertElement("line-item", poUri);

	  addTopicInfo(envelopeDocument);
	  bodyCursor.toPrevToken();
	  bodyCursor.insertElementWithText("price", poUri, "300");
	  bodyCursor.dispose();

	  return envelopeDocument;
  }
	  
  
  private void addTopicInfo(EnvelopeDocument envelopeDocument) {
	  String topicName = "/Literature/Shakespere";
	  WseQNames wseQNames = WseQNames.getInstance();
	  QName qName = wseQNames.getTopicQNameNBX();
	  boolean added = soapMessageAlteration.addToSoapHeader(envelopeDocument, 
			  qName, topicName);

	  if (!added) {
	    System.out.println(moduleName + "Addition of Topic info NOT Successful");
	  }
  }
  
  
  public AddressingHeaders parseWsaHeaders() throws Exception {
    AddressingHeaders addressingHeaders =
      parseWsaHeaders.getAddressingHeaders(envelopeDocumentForParsingWsaHeaders);
    
    return addressingHeaders;
  }


  public void createSequenceTerminatedFault() {
    EndpointReferenceType faultTo = eprForWsaEnvelope;
    String id = "uuid: e0987-fgrt7-98716sdg-456ghty";
    cgl.narada.wsinfra.schemas.utility.IdentifierDocument idDocument = 
      cgl.narada.wsinfra.schemas.utility.IdentifierDocument.Factory.newInstance();
    idDocument.addNewIdentifier().setStringValue(id);

    SequenceFaultDocument sequenceFaultDocument = 
      wsrmFaults.getSequenceTerminated(id);

        
    WsFaultException wsFaultException = 
      new WsFaultException(wsrmFaults.getSoapSenderQName(), 
			   wsrmFaults.getSequenceTerminatedReason());

    wsFaultException.setDetailObject(idDocument);
    wsFaultException.setAddSoapHeaderElement(sequenceFaultDocument);
    wsFaultException.setFaultTo(faultTo);

    EnvelopeDocument faultDocument = 
      faultConverter.convertToSOAPFault(wsFaultException);
    
    //System.out.println(faultDocument.getEnvelope() );
  }


  public EnvelopeDocument createCreateSequenceRequest() {
    EnvelopeDocument envelopeDocument =
      wsrmRequestCreator.getCreateSequenceRequest(wsrmToDocument,
						  eprForWsaEnvelope);
    return envelopeDocument;
  }


  public EnvelopeDocument createCreateSequenceResponse() {
    EnvelopeDocument envelopeDocument =
      wsrmResponseCreator.getCreateSequenceResponse(addressingHeaders,
						    eprForWsaEnvelope,
						    wsrmSequenceIdentifier);
    return envelopeDocument;
  }


  public SequenceDocument createSequenceDocument() {
    long messageNumber = 1;
    SequenceDocument sequenceDocument =
      wsrmElementCreation.newSequence(wsrmSequenceIdentifier, messageNumber);
    return sequenceDocument;
  }


  public void addSequenceInfoToEnvelope() {
    wsrmElementAddition.addSequence(envelopeDocumentForConversion, 
				    wsrmSequenceDocument);
  }


  public SequenceAcknowledgementDocument createSequenceAcknowledgement() {
    SequenceAcknowledgementDocument sequenceAckDocument =
      wsrmElementCreation.newSequenceAcknowledgement(wsrmSequenceIdentifier, 
						     wsrmMessageNumbersToAck,
						     true);
    return sequenceAckDocument;
  }

  public TerminateSequenceDocument createTerminateSequence() {
    TerminateSequenceDocument terminateSeqDocument =
      wsrmElementCreation.newTerminateSequence(wsrmSequenceIdentifier);
    return terminateSeqDocument;
  }

}





